//
//  JWNewsPictureViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/21.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWNewsPictureViewController.h"
#import "JWNewsPictureModel.h"
#import "JWNewsPictureCell.h"
#import "JWBrowseViewController.h"
#import "JW_BrowseViewController.h"
#import "BaseNavigationController.h"

@interface JWNewsPictureViewController ()

@property (nonatomic, strong) NSArray *newsPictureAr;

@end

@implementation JWNewsPictureViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    self.title = @"图片新闻";
    
    
    
    // Register cell classes

    [self.collectionView registerClass:[JWNewsPictureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSArray *)newsPictureAr{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:self.jsonName ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:path];
    NSMutableArray *jsonAr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    
    if (!_newsPictureAr) {
        NSMutableArray *tempAr = [NSMutableArray array];
        for (int i = 0; i < jsonAr.count; i++) {
            NSDictionary *newsPictureInfoDic = jsonAr[i];
            JWNewsPictureModel *mode = [JWNewsPictureModel newsPictureModelWithDictionary:newsPictureInfoDic];
            
            [tempAr addObject:mode];
        }
        _newsPictureAr = tempAr;
        
    }
    
    return _newsPictureAr;
    
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.newsPictureAr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    JWNewsPictureCell *cell = [JWNewsPictureCell newsPictureCellWithCollectionView:collectionView ReuseIdentifier:reuseIdentifier indexPath:indexPath];
    JWNewsPictureModel *model = self.newsPictureAr[indexPath.row];
    
    cell.newsPictureModel = model;

    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/
//
#pragma mark <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size = CGSizeMake(90, 90);
    return size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5.f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5.f;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    /*-------------------------------scrollView----------------------------------------*/
    
//    JWBrowseViewController *browseVC = [[JWBrowseViewController alloc] init];
//    browseVC.clickTag = indexPath.row;
//    browseVC.imageAr = self.newsPictureAr;
//    
//    [self presentViewController:browseVC animated:YES completion:nil];
    
    
    /*-------------------------------collectionView----------------------------------------*/
    
    JW_BrowseViewController *browseVC = [[JW_BrowseViewController alloc] init];
    
    BaseNavigationController *nvg = [[BaseNavigationController alloc] initWithRootViewController:browseVC];
    
    browseVC.clickTag = indexPath.row;
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < self.newsPictureAr.count; i++) {
        JWNewsPictureModel *model = self.newsPictureAr[i];
        NSString *imageUrlStr = model.image;
        [array addObject:imageUrlStr];
    }
    browseVC.imageUrlAr = [array copy];
    
    

    [self presentViewController:nvg animated:YES completion:nil];
      
}


@end

